package com.lcz.authority.admin.controller.system;

import com.lcz.authority.admin.service.SysDictDataService;
import com.lcz.authority.admin.service.SysDictTypeService;
import com.lcz.authority.common.annotation.Anonymous;
import com.lcz.authority.common.annotation.ApiResource;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.R;
import com.lcz.authority.common.domain.entity.SysDictDataEntity;
import com.lcz.authority.common.enums.AppCodeEnum;
import com.lcz.authority.common.enums.ResBizTypeEnum;
import com.lcz.authority.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lcz
 * @desc 字典数据管理控制器
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */

@RestController
@RequestMapping("/system/dict/data")
@ApiResource(name = "字典数据管理", resBizType = ResBizTypeEnum.SYSTEM, appCode = AppCodeEnum.SYSTEMMODULES)
@Api(tags = "字典数据管理")
public class SysDictDataController {
    @Autowired
    private SysDictDataService dictDataService;
    @Autowired
    private SysDictTypeService dictTypeService;


    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping(value = "/list", name = "字典数据管理-分页")
    @ApiOperation("分页")
    public R page(SysDictDataEntity dictData) {
        PageResult<SysDictDataEntity> page = dictDataService.page(dictData);
        return R.ok().put(page);
    }

    @GetMapping(value = "/type/{dictType}", name = "字典数据管理-根据字典类型查询字典数据信息")
    @Anonymous
    @ApiOperation("根据字典类型查询字典数据信息")
    public R dictType(@PathVariable String dictType) {
        List<SysDictDataEntity> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjectUtils.isEmpty(data)) {
            data = new ArrayList<>();
        }
        return R.ok().put(data);
    }

    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}", name = "字典数据管理-查询")
    @ApiOperation("查询")
    public R getInfo(@PathVariable String dictCode) {
        return R.ok(dictDataService.selectDictDataById(dictCode));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping(name = "字典数据管理-新增")
    @ApiOperation("新增")
    public R add(@Validated @RequestBody SysDictDataEntity dict) {
        return R.ok(dictDataService.insertDictData(dict));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping(name = "字典数据管理-修改")
    @ApiOperation("修改")
    public R edit(@Validated @RequestBody SysDictDataEntity dict) {
        return R.ok(dictDataService.updateDictData(dict));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping(value = "/{dictCodes}", name = "字典数据管理-删除")
    @ApiOperation("删除")
    public R remove(@PathVariable String[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @PostMapping(value = "/export", name = "字典数据管理-导出")
    @ApiOperation("导出字典数据数据")
    public void export(HttpServletResponse response, SysDictDataEntity dict) {
        List<SysDictDataEntity> list = dictDataService.page(dict).getRows();
        ExcelUtil<SysDictDataEntity> util = new ExcelUtil<>(SysDictDataEntity.class);
        util.exportExcel(response, list, "字典数据管理");
    }
}
